package hlhj.fhp.supreme.customview

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.ClassTypeBean
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.class_type_pop.view.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\4\28 0028.
 */
class ClassTypePop(private val context:Context,private val datas:ArrayList<ClassTypeBean.DataBean.CatBean>,private val listnerer: OnClassTypeClickListnerer)  :PopupWindow(){
    private lateinit var adp:BaseQuickAdapter<ClassTypeBean.DataBean.CatBean,BaseViewHolder>
    init {
        initPop()
        this.width=LinearLayout.LayoutParams.WRAP_CONTENT
        this.height=LinearLayout.LayoutParams.WRAP_CONTENT
    }

    private fun initPop() {
        contentView=LayoutInflater.from(context).inflate(R.layout.class_type_pop,null)

        adp=object :BaseQuickAdapter<ClassTypeBean.DataBean.CatBean,BaseViewHolder>(R.layout.class_type_item,datas){
            override fun convert(helper: BaseViewHolder?, item: ClassTypeBean.DataBean.CatBean?) {
                helper?.setText(R.id.itemTittle,item?.name)
                val lo1 = helper?.getView<RelativeLayout>(R.id.lo1)
                val lis = helper?.getView<RecyclerView>(R.id.listView)

                lo1?.setOnClickListener {
//                    MyUtils.toast("外侧点击")
                    if (lis?.visibility==View.GONE){
                        lis?.visibility=View.VISIBLE
                        lis?.adapter=object :BaseQuickAdapter<ClassTypeBean.DataBean.CatBean.LevelBean,BaseViewHolder>(R.layout.class_type_item_item,item?.level){
                            override fun convert(helper: BaseViewHolder?, itt: ClassTypeBean.DataBean.CatBean.LevelBean?) {
                                        helper?.setText(R.id.tv,itt?.title)
                                val tv = helper?.getView<TextView>(R.id.tv)
                                tv?.setOnClickListener {
                                    listnerer.onClassClick(item?.id.toString(),itt?.id.toString(),item?.name+"-"+itt?.title)
                                    dismiss()
                                }
                            }
                        }
                        lis?.layoutManager=LinearLayoutManager(mContext,LinearLayout.VERTICAL,false)

                    }else{
                        lis?.visibility=View.GONE
                    }
                }
            }
        }

        contentView.listView.adapter=adp
        contentView.listView.layoutManager=LinearLayoutManager(context,LinearLayout.VERTICAL,false)

    }

    interface OnClassTypeClickListnerer {
        fun onClassClick(cid:String,lid:String,name:String)
    }

}